package com.wencst.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.util.ResourceUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wencst.security.bean.Menu;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JsonData {
	public List<Menu> jsonDataString() {
		List<Menu> cmpNos = new ArrayList<Menu>();
		try {
			File file = ResourceUtils.getFile("classpath:templates/json/menus.json");
			String jsonData = FileUtils.readFileToString(file,
					Charset.forName("UTF-8"));
			cmpNos = JSONObject.parseArray(jsonData, Menu.class);
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
		return cmpNos;
	}

	public List<Menu> jsonDataString1() {
		List<Menu> cmpNos = new ArrayList<Menu>();
		try {
			URL url = ClassLoader.getSystemResource("templates/json/menus.json");
			File file = new File(url.getFile());
			String jsonData = FileUtils.readFileToString(file,
					Charset.forName("UTF-8"));
			cmpNos = JSONObject.parseArray(jsonData, Menu.class);
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
		return cmpNos;
	}
	public void jsonToFile(List<Menu> menus) {
		String menusJson = JSON.toJSONString(menus);
		try {
			URL url = ClassLoader.getSystemResource("templates/json/menus1.json");
			File file = new File(url.getFile());
//			File file = ResourceUtils.getFile("classpath:templates/json/menus1.json");
			FileUtils.writeStringToFile(file, "1234", Charset.forName("UTF-8"));
			FileUtils.write(file, "12345", Charset.forName("UTF-8"));
			List<String> content = new ArrayList<String>();
			content.add(menusJson);
			FileUtils.writeLines(file, "UTF-8", content);
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
	}
	public void jsonToFile1(List<Menu> menus) {
		OutputStream out = null;
		String menusJson = JSON.toJSONString(menus);
        try {
        	File file = ResourceUtils.getFile("classpath:templates/json/menus1.json");
        	log.info(file.getAbsolutePath());
            out = FileUtils.openOutputStream(file, true);
//            IOUtils.write(menusJson, out, Charset.forName("UTF-8"));
            out.write(menusJson.getBytes());
            out.close(); // don't swallow close Exception if copy completes normally
        } catch (IOException e) {
        	log.error(e.getMessage(),e);
		} finally {
            IOUtils.closeQuietly(out);
        }
	}

	public static void main(String[] args) throws FileNotFoundException {
		JsonData jd = new JsonData();
		List<Menu> menus = jd.jsonDataString();
		String menusJson = JSON.toJSONString(menus);
		for(Menu menu: menus) {
			System.out.println(menu.toString());
		}
		System.out.println(menusJson);
		try {
			URL file1 = ClassLoader.getSystemResource("templates/json/menus.json");
			File file2 = new File(file1.getFile());
			File file = ResourceUtils.getFile("classpath:templates/json/menus.json");
			FileUtils.writeStringToFile(file, menusJson, Charset.forName("UTF-8"), false);
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
	}
}
